import { Dictionary } from "../Support/Dictionary";
import { PlayerVO } from "../VO/Player/PlayerVO";
import RoomVO from "../VO/Game/RoomVO";
import GameVO from "../VO/Game/GameVO";
import ConfigVO from "../VO/Game/ConfigVO";

export class GameDataManager {
    private static _instance: GameDataManager = null;
    public static get instance(): GameDataManager {
        if (!this._instance) this._instance = new GameDataManager();
        return this._instance;
    }
    private dict: Dictionary<string, object>;
    private _player: PlayerVO;
    private _room: RoomVO;
    private _game: GameVO;
    private _config : ConfigVO

    public constructor() {
        this._player = new PlayerVO();
        this._room = new RoomVO();
        this._game = new GameVO();
        this._config = new ConfigVO();

        this.dict = new Dictionary();
    }
    /**获取到字典数据**/
    public getDictData(key: string): any { return this.dict.getValue(key); }
    /**设置字典数据* */
    public setDictData(key: string, value: any): void { this.dict.setValue(key, value); }


    public setShareData(key: string, value: any): void {
        cc.sys.localStorage.setItem(key, JSON.stringify(value));
    }
    public getShareData(key: string, clz: any, defaultValue?: any): any {
        let str: string = cc.sys.localStorage.getItem(key);
        if (str == null || str.length == 0) {
            if (clz != null) return new clz();
            else {
                if (defaultValue != null) return defaultValue;
                else return null;
            }
        } else {
            if (clz == null) return JSON.parse(str);
            else {
                var obj: object = new clz();
                var json: object = JSON.parse(str);
                obj["update"](json);
                return obj;
            }
        }
    }

    public static get player(): PlayerVO { return this.instance._player; }

    public static set room(r: RoomVO) { this.instance._room = r };
    public static get room(): RoomVO { return this.instance._room; }

    public static set game(g: GameVO) { this.instance._game = g };
    public static get game(): GameVO { return this.instance._game; }

    //public static set config(g: GameVO) { this.instance._game = g };
    public static get  config(): ConfigVO { return this.instance._config; }
}